home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / misc / volume5 / random < prev    next >
Encoding:
Internet Message Format  |  1989-02-03  |  3.4 KB

  1. Path: xanth!nic.MR.NET!hal!ncoast!allbery
  2. From: fuat@cunixc.cc.columbia.edu (Fuat C. Baran)
  3. Newsgroups: comp.sources.misc
  4. Subject: v05i009: Minimal Standard Random Number Generator
  5. Keywords: random number generator
  6. Message-ID: <1057@cunixc.columbia.edu>
  7. Date: 28 Oct 88 02:31:20 GMT
  8. Sender: allbery@ncoast.UUCP
  9. Reply-To: fuat@cunixc.cc.columbia.edu (Fuat C. Baran)
  10. Organization: Columbia University Center for Computing Activities
  11. Lines: 122
  12. Approved: allbery@ncoast.UUCP
  13.  
  14. Posting-number: Volume 5, Issue 9
  15. Submitted-by: "Fuat C. Baran" <fuat@cunixc.cc.columbia.edu>
  16. Archive-name: random
  17.  
  18. [Conflict!!!  BSD already has a random() which is better than rand().  ++bsa]
  19.  
  20. I don't know if this is really worth posting (I just converted
  21. pseudo-code to C from the latest issue of CACM), but in case other
  22. people need this I thought I'd make it available.  It implements a
  23. "minimal standard random number generator" discussed in CACM.  It is
  24. meant as a replacement for rand(3) that comes with 4.x BSD which is
  25. said to be a poor generator according to the article.  It should work
  26. on any system in which the maximum integer is 2^31-1 (when compiled
  27. with -DTEST it checks to see whether or not it was implemented
  28. correctly).
  29.  
  30.                     --Fuat
  31.  
  32. #! /bin/sh
  33. # This is a shell archive, meaning:
  34. # 1. Remove everything above the #! /bin/sh line.
  35. # 2. Save the resulting text in a file.
  36. # 3. Execute the file with /bin/sh (not csh) to create:
  37. #    random.c
  38. # This archive created: Mon Oct 24 17:27:26 1988
  39. export PATH; PATH=/bin:/usr/bin:$PATH
  40. if test -f 'random.c'
  41. then
  42.     echo shar: "will not over-write existing file 'random.c'"
  43. else
  44. sed 's/^X//' << \SHAR_EOF > 'random.c'
  45. X/**
  46. X ** random.c: Minimal Standard Pseudo-Random Number Generator
  47. X **
  48. X ** Author: Fuat C. Baran, Columbia University, 1988
  49. X **
  50. X ** Based on code in "Random Number Generators: Good Ones are Hard to Find",
  51. X ** by Stephen K. Park and Keith W. Miller in Communications of the ACM,
  52. X ** 31, 10 (Oct. 1988) pp. 1192-1201.
  53. X **
  54. X ** Requirements: maxint must be 2^31 -1 or larger.
  55. X **
  56. X ** Compile with -DTEST and run to see if it was implemented correctly.
  57. X **/
  58. X
  59. X/* some constants we need */
  60. X#define A 16807
  61. X#define M 2147483647            /* Mersenne prime 2^31 -1 */
  62. X#define Q 127773            /* M div A (M / A) */
  63. X#define R 2836                /* M mod A (M % A) */
  64. X
  65. Xstatic long seed = 1;            /* initialize with rand_init() */
  66. X
  67. X
  68. X/*
  69. X * random:
  70. X * minimal standard random number generator.
  71. X * call rand_init first to initialize seed.
  72. X * returns a random float.
  73. X */
  74. X
  75. Xfloat
  76. Xrandom () {
  77. X    long hi, lo;
  78. X    
  79. X    hi = seed / Q;
  80. X    lo = seed % Q;
  81. X    if ((seed = A * lo - R * hi) <= 0)
  82. X        seed += M;
  83. X    return ((float) seed / M);
  84. X}
  85. X
  86. X
  87. X/*
  88. X * rand_init:
  89. X * initialize seed.
  90. X */
  91. X
  92. Xvoid
  93. Xrand_init (s)
  94. Xlong s;
  95. X{
  96. X    seed = s;
  97. X}
  98. X
  99. X
  100. X#ifdef TEST
  101. X
  102. Xmain () {
  103. X    int i;
  104. X    float r;
  105. X
  106. X    rand_init (1);
  107. X    for (i = 0; i < 10000; i++)
  108. X    r = random();
  109. X    printf ("Seed is %ld (should be 1043618065)\n", seed);
  110. X}
  111. X
  112. X#else
  113. X
  114. Xmain () {
  115. X    int i;
  116. X
  117. X    for (i = 0; i < 1000000; i++)
  118. X    random();
  119. X}
  120. X
  121. X#endif /* TEST */
  122. X
  123. X
  124. SHAR_EOF
  125. fi
  126. exit 0
  127. #    End of shell archive
  128.  
  129.  
  130.  
  131. -- 
  132. INTERNET: fuat@columbia.edu          U.S. MAIL: Columbia University
  133. BITNET:   fuat@cunixc.cc.columbia.edu           Center for Computing Activities
  134. USENET:   ...!rutgers!columbia!cunixc!fuat      712 Watson Labs, 612 W115th St.
  135. PHONE:    (212) 280-5128                        New York, NY 10025
  136.